package com.aabte.springcloudlearn.ribbonhystrixcustomerdemo.controller;

import com.aabte.springcloudlearn.ribbonhystrixcustomerdemo.service.CacheCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author Daniel
 */
@Slf4j
@RestController
public class CacheCustomerController {

    @Autowired
    private CacheCustomerService service;

    @GetMapping("/cacheOn")
    public void openCache(){
        log.info("Start openCache.");
        // 初始化Hystrix请求上下文
        // 生产环境使用filter的方式初始化
        // HystrixRequestContext context = HystrixRequestContext.initializeContext();
        // 生成缓存Key
        String cacheKey = UUID.randomUUID().toString().replace("-", "");
        log.info("cacheKey: {}", cacheKey);
        // 开户请求缓存并测试两次
        service.openCacheByExtends(cacheKey);
        // 清除缓存
        service.clearCacheByExtends(cacheKey);
        // 再次再启请求缓存并测试两次
        service.openCacheByExtends(cacheKey);

        // context.close();
        log.info("End openCache.");
    }

    @GetMapping("/cacheAnnotation1")
    public void openCacheByAnnotation1(){
        log.info("Start openCacheByAnnotation1.");
        // 初始化Hystrix请求上下文
        // 生产环境使用filter的方式初始化
        // HystrixRequestContext context = HystrixRequestContext.initializeContext();
        // 生成缓存Key
        String cacheKey = UUID.randomUUID().toString().replace("-", "");
        log.info("cacheKey: {}", cacheKey);
        // 开户请求缓存并测试两次
        Integer result1 = service.openCacheByAnnotation1(cacheKey);
        Integer result2 = service.openCacheByAnnotation1(cacheKey);
        log.info("first request result:{} ,and secend request result: {}", result1, result2);
        // 清除缓存
        service.flushCacheByAnnotation1(cacheKey);
        // 再次再启请求缓存并测试两次
        Integer result3 = service.openCacheByAnnotation1(cacheKey);
        Integer result4 = service.openCacheByAnnotation1(cacheKey);
        log.info("first request result:{} ,and secend request result: {}", result3, result4);

        // context.close();
        log.info("End openCacheByAnnotation1.");
    }


    @GetMapping("/cacheAnnotation2")
    public void openCacheByAnnotation2(){
        log.info("Start openCacheByAnnotation2.");
        // 初始化Hystrix请求上下文
        // 生产环境使用filter的方式初始化
        // HystrixRequestContext context = HystrixRequestContext.initializeContext();
        // 生成缓存Key
        String cacheKey = UUID.randomUUID().toString().replace("-", "");
        log.info("cacheKey: {}", cacheKey);
        // 开户请求缓存并测试两次
        Integer result1 = service.openCacheByAnnotation2(cacheKey);
        Integer result2 = service.openCacheByAnnotation2(cacheKey);
        log.info("first request result:{} ,and secend request result: {}", result1, result2);
        // 清除缓存
        service.flushCacheByAnnotation2(cacheKey);
        // 再次再启请求缓存并测试两次
        Integer result3 = service.openCacheByAnnotation2(cacheKey);
        Integer result4 = service.openCacheByAnnotation2(cacheKey);
        log.info("first request result:{} ,and secend request result: {}", result3, result4);

        // context.close();
        log.info("End openCacheByAnnotation2.");
    }


    @GetMapping("/cacheAnnotation3")
    public void openCacheByAnnotation3(){
        log.info("Start openCacheByAnnotation3.");
        // 初始化Hystrix请求上下文
        // 生产环境使用filter的方式初始化
        // HystrixRequestContext context = HystrixRequestContext.initializeContext();
        // 生成缓存Key
        String cacheKey = UUID.randomUUID().toString().replace("-", "");
        log.info("cacheKey: {}", cacheKey);
        // 开户请求缓存并测试两次
        Integer result1 = service.openCacheByAnnotation3(cacheKey);
        Integer result2 = service.openCacheByAnnotation3(cacheKey);
        log.info("first request result:{} ,and secend request result: {}", result1, result2);
        // 清除缓存
        service.flushCacheByAnnotation2(cacheKey);
        // 再次再启请求缓存并测试两次
        Integer result3 = service.openCacheByAnnotation3(cacheKey);
        Integer result4 = service.openCacheByAnnotation3(cacheKey);
        log.info("first request result:{} ,and secend request result: {}", result3, result4);

        // context.close();
        log.info("End openCacheByAnnotation3.");
    }

}
